package controller;

import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.History;
import service.HistoryService;
import service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("history")
public class HistoryController {
    @Resource
    private HistoryService historyService;

    @RequestMapping("insert")
    public void insert(@RequestParam Map<String, String> map){

        System.out.println("调用历史记录添加");

/*        String HistoryStr=map.get("HistoryStr");*/
        String userid=map.get("userid");
        String blogid=map.get("blogId");
/*        Gson gson=new Gson();
        History history=gson.fromJson(HistoryStr,History.class);*/
   /*     List<History> list= historyService.jundgeBlogid(blogid);
        String JsonStr=new Gson().toJson(list);*/

        if(userid!=null){
          /*  System.out.println(HistoryStr);*/
        historyService.insert(userid,blogid);
        }
    }

    @RequestMapping("getHistory")
    public void getHistory(@RequestParam Map<String, String> map, HttpServletResponse response) throws IOException {
        System.out.println("浏览历史记录被调用");
        String userid=map.get("userid");
        List<History> list=historyService.getHistory(userid);

            String JsonStr=new Gson().toJson(list);
            response.setCharacterEncoding("utf-8");
            System.out.println("历史记录"+JsonStr);
            response.getWriter().write(JsonStr);


    }
}
